package com.sxt.business.service.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxt.business.domain.Customer;
import com.sxt.business.domain.Goods;
import com.sxt.business.domain.Sales;
import com.sxt.business.mapper.SalesMapper;
import com.sxt.business.service.ICustomerService;
import com.sxt.business.service.IGoodsService;
import com.sxt.business.service.ISalesService;
import com.sxt.business.vo.SalesVO;
import com.sxt.system.utils.DataGridView;
import com.sxt.system.utils.WebAppUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 俊哥
 * @since 2019-11-16
 */
@Service
public class SalesServiceImpl extends ServiceImpl<SalesMapper, Sales> implements ISalesService {

	@Autowired
	private ICustomerService customerService ;
	
	/**
	 * 	分页全查询
	 */
	@Override
	public DataGridView queryAllSales(SalesVO salesVO) {
		Page<Sales> page = new Page<Sales>(salesVO.getPage(), salesVO.getLimit());
		
		QueryWrapper<Sales> qw = new QueryWrapper<Sales>();
		qw.eq(salesVO.getCustomerid()!=null, "customerid", salesVO.getCustomerid());
		qw.eq(salesVO.getGoodsid()!=null, "goodsid", salesVO.getGoodsid());
		qw.ge(salesVO.getStartTime() != null, "inporttime", salesVO.getStartTime());
		qw.le(salesVO.getEndTime() != null, "inporttime", salesVO.getEndTime());
		qw.orderByDesc("salestime");
		this.getBaseMapper().selectPage(page, qw);
		List<Sales> list = page.getRecords();
		for (Sales sales : list) {
			Integer customerid = sales.getCustomerid();
			Integer goodsid = sales.getGoodsid();
			if(customerid != null) {
				Customer customer = this.customerService.getById(customerid);
				sales.setCustomername(customer.getCustomername());
			}
			if(null!=goodsid) {
				Goods goods = WebAppUtils.getContent().getBean(IGoodsService.class).getById(goodsid);
				sales.setGoodsname(goods.getGoodsname());
				sales.setSize(goods.getSize());
			}
		}
		return new DataGridView(page.getTotal(), list);
	}

	@Override
	public Sales addSales(Sales sales) {
		this.getBaseMapper().insert(sales);
		//减少商品库存
		IGoodsService iGoodsService = WebAppUtils.getContent().getBean(IGoodsService.class);
		Goods goods = iGoodsService.getById(sales.getGoodsid());
		goods.setNumber(goods.getNumber()-sales.getNumber());
		iGoodsService.updateById(goods);
		return sales;
	}

	@Override
	public Sales updateSales(Sales sales) {
		//查询的销售单
		Sales oldSales = this.getBaseMapper().selectById(sales.getId());
		IGoodsService iGoodsService = WebAppUtils.getContent().getBean(IGoodsService.class);
		Goods goods = iGoodsService.getById(sales.getGoodsid());
		//总库存-旧订单的库存+新修改之后的数量
		goods.setNumber(goods.getNumber()+oldSales.getNumber()-sales.getNumber());
		iGoodsService.updateById(goods);
		this.getBaseMapper().updateById(sales);
		return sales;
	}

	@Override
	public boolean removeById(Serializable id) {
		Sales sales = this.getBaseMapper().selectById(id);
		IGoodsService iGoodsService = WebAppUtils.getContent().getBean(IGoodsService.class);
		Goods goods = iGoodsService.getById(sales.getGoodsid());
		goods.setNumber(goods.getNumber()+sales.getNumber());
		iGoodsService.updateById(goods);
		return super.removeById(id);
	}
	
	
}
